<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<title>业务处理-退货申请-编辑退货申请</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
</head>
<body>
	<div id="mainContent">
		<div id="mainContent_content">
			<div class="page_header" >
			<a href="<%=basePath %>returnRequest/listReturnRequest.do">退货申请</a>
			<i class="icon-double-right"></i><a>退货申请单</a>
			</div>
			<div class="btn rightBtn">
				<c:if test="${returnRequest.status==0 && isEdit }">
				<input type="button" class="btn btn1" value="新增物品" onclick="toAddReturnRequestItem();"></input>
				<input type="button" class="btn btn2" value="保存" onclick="saveEditReturnRequest();"></input>
				<input type="button" class="btn btn1" value="提交" onclick="saveAndSubmit();"></input>
				</c:if>
				<input type="button" class="btn btn1" value="下载PDF" onclick="window.open('<%=basePath %>returnRequest/download.do?returnRequestID=${returnRequest.returnRequestID }');"></input>
				<input type="button" class="btn btn2" value="返回" onclick="location.href='<%=basePath %>returnRequest/listReturnRequest.do'"></input>
				
			</div>
			<form id="returnRequestDetailForm" class="form">
			<div id="requisitionForm" class="box">
				<input type="hidden" name="returnRequestID" readonly value="${returnRequest.returnRequestID }"></input>
				<p>
					<label>申请单号</label>
					<input type="text" class="form-control" readonly value="${returnRequest.applicationNo }"></input>
					<label>申请人</label>
					<input type="text" class="form-control" readonly value="${returnRequest.createPersonName }"></input>
					<label>状态</label>
					<c:if test="${returnRequest.status==0 }"><input readonly type="text" class="form-control" value="未提交"></c:if>
					<c:if test="${returnRequest.status==1 }"><input readonly type="text" class="form-control" value="已提交"></c:if>
					<c:if test="${returnRequest.status==2 }"><input readonly type="text" class="form-control" value="审核中"></c:if>
					<c:if test="${returnRequest.status==3 }"><input readonly type="text" class="form-control" value="已通过"></c:if>
					<c:if test="${returnRequest.status==4 }"><input readonly type="text" class="form-control" value="未通过"></c:if>
				</p>
				<p>
					<label>仓库</label>
					<select name="warehouseManagementID" class="form-control select" <c:if test="${!isEdit }">disabled</c:if> onchange="clearItem();">
						<c:forEach items="${warehouseList }" var="warehouse">
							<option value="${warehouse.warehousemanagementid }" <c:if test="${warehouse.warehousemanagementid==returnRequest.warehouseManagementID }">selected</c:if>>${warehouse.warehousename }</option>
						</c:forEach>
					</select>
					<label>部门</label>
					<select name="organizationID" class="form-control select" <c:if test="${!isEdit }">disabled</c:if>>
						<option value=""></option>
						<c:forEach items="${organizations }" var="org">
							<option value="${org.organizationid }" <c:if test="${org.organizationid==returnRequest.organizationID }">selected</c:if>>${org.organizationname }</option>
						</c:forEach>
					</select>
					<label>邮轮</label>
					<select name="cruiseID" class="form-control select" <c:if test="${!isEdit }">disabled</c:if>>
						<option value=""></option>
						<c:forEach items="${cruiseList }" var="cruise">
							<option value="${cruise.cruiseID }"  <c:if test="${cruise.cruiseID==returnRequest.cruiseID }">selected</c:if>>${cruise.cruiseName }</option>
						</c:forEach>
					</select>
				</p>
				<p>
					<label>说明</label>
					<input type="text" class="form-control trebleWidth" <c:if test="${returnRequest.status==1 }">readonly</c:if> name="returnPolicy" value="${returnRequest.returnPolicy}"></input>
				</p>
			</div>
			<div class="tableBox box">
				<table class="table table-bordered table_text_center">
					<thead>
						<tr>
							<th>物品批次</th>
							<th>物品名称</th>
							<th>物品条码</th>
							<th>品牌</th>
							<th>类别</th>
							<c:if test="${returnRequest.status==0 && isEdit }">
								<th>库存</th>
							</c:if>
							<th>退货数量</th>
							<c:if test="${returnRequest.status==0 && isEdit }">
								<th>小计</th>
							</c:if>
							<c:if test="${returnRequest.status==0 && isEdit }">
								<th>操作</th>
							</c:if>
						</tr>
					</thead>
					<tbody id="returnRequestItemTbody">
						<c:forEach items="${itemList }" var="item">
								<tr>
									<td>${item.batchNumber }</td>
									<td>${item.chineseName }</td>
									<td>${item.articleBarCode }</td>
									<td>${item.brand }</td>
									<td>${item.categoryName }</td>
									<c:choose>
										<c:when test="${returnRequest.status==0 && isEdit }">
											<td>${item.max }</td>
											<td><input type="text" class="form-control" name="unitNum" value="${item.number }"
												data-max="${item.max }" onchange="numberChange(this);"
												onkeyup="numberChange(this);">
												<select class="form-control select" onchange='numberChange(this);' name='unit'>
												<option value='${item.salesSpecifications }'>${item.salesUnit }</option>
												<option value='${item.purchaseSpecifications }'>${item.purchaseSpecifications}${item.salesUnit}/${item.purchasingUnit}</option>
												</select>
											</td>
											<td><input type='text' class="form-control" name='number' readonly value='${item.number }'/></td>
											<td><a class="btn btn-xs btn_danger" onclick="deleteItem(this);">删除</a><input
												type="hidden" name="itemIds" value="${item.sourceID }"></td>
										</c:when>
										<c:otherwise>
											<td>${item.number }</td>
										</c:otherwise>
									</c:choose>
								</tr>
						<%-- 	</c:if> --%>
						</c:forEach>
					</tbody>
				</table>
			</div>
			<div class="box updateBox">
				<p>
					<label>提交人</label>
					<input type="text" class="form-control" readonly value="${returnRequest.submitterName }"></input>
					<label>提交时间</label>
					<input type="text" class="form-control" readonly value="<fmt:formatDate value='${returnRequest.commitTime }' type='date'/>"></input>
				</p>
				<p>
					<label>创建时间</label>
					<input type="text" class="form-control" readonly value="<fmt:formatDate value='${returnRequest.createDate }' type='date'/>"></input>
					<label>最后处理人</label>
					<input type="text" class="form-control" readonly value="${returnRequest.lastModifiedName }"></input>
					<label>最后处理时间</label>
					<input type="text" class="form-control" readonly value="<fmt:formatDate value='${returnRequest.lastModifiedTime }' type='date'/>"></input>
				</p>
			</div>
			</form>
		</div>
		<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
		<jsp:include page="/jsp/business/returnRequest/addRequestItem.jsp"></jsp:include>
	</div>
	<script src="<%=basePath %>js/jquery-3.2.1.min.js"></script>
	<script src="<%=basePath %>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="<%=basePath %>My97DatePickers/WdatePicker.js"></script>
	<script src="<%=basePath %>js/public.js"></script>
	<script type="text/javascript">
	function addToHasAddTable(item){
		var html="<tr>";
		html+="<td>"+item.batchNumber+"</td>";
		html+="<td>"+item.chineseName+"</td>";
		html+="<td>"+item.articleBarCode+"</td>";
		html+="<td>"+item.brand+"</td>";
		html+="<td>"+item.categoryName+"</td>";
		html+="<td>"+item.number+"</td>";
		html+="<td><input type='text' class='form-control' name='unitNum' value='"+item.number+"' data-max='"+item.number+"' onchange='numberChange(this);' onkeyup='numberChange(this);'/>";
		html+="<select class='form-control select' onchange='numberChange(this);' name='unit'><option value='"+item.salesSpecifications+"'>"+item.salesUnit+"</option><option value='"+item.purchaseSpecifications+"'>"+item.purchaseSpecifications+item.salesUnit+"/"+item.purchasingUnit+"</option></select></td>";
		html+="<td><input type='text' class='form-control' name='number' readonly value='"+item.number+"'/></td>";
		html+="<td><a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>";
		html+="<input type='hidden' name='itemIds' value='"+item.id+"' /></td>";
		html+="</tr>";
		$("#returnRequestItemTbody").append(html);
	}
	
	function numberChange(tag){
		var obj=$(tag).parent().parent().find("input[name='unitNum']")[0];
		obj.value=obj.value.replace(/\D/g,'');
		var num=obj.value;
		var unit=$(obj).parent().parent().find("select[name='unit']").val();
		var total=num*unit;
		var max=$(obj).data("max");
		if(total>max){
			num=parseInt(max/unit);
			total=num*unit;
		}
		obj.value=num;
		$(obj).parent().parent().find("input[name='number']").val(total);
	}
	
	function deleteItem(obj){
		$(obj).parent().parent().remove();
	}
	
	function clearItem(){
		$("#returnRequestItemTbody").html("");
	}
	
	function validateForm(){
		var cruiseID=$("#returnRequestDetailForm [name='cruiseID']").val();
		if(cruiseID==''){
			Prompt.alert("提示","请选择邮轮！")
			return false;
		}
		var length=$("#returnRequestItemTbody [name='itemIds']").length;
		if(!length>0){
			Prompt.alert("提示","物品种类数量必须大于0！")
			return false;
		}
		var arr=$("#returnRequestItemTbody [name='number']");
		for(var i=0;i<arr.length;i++){
			var num=$(arr[i]).val();
			if(num.trim()=='' || num<=0){
				Prompt.alert("提示","物品退货数量必须大于0！")
				return false;
			}
		}
		return true;
	}
	
	function saveEditReturnRequest(){
		if(!validateForm()) return ;
		$.ajax({
			url:"<%=basePath %>returnRequest/edit.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#returnRequestDetailForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购申请保存成功！",function(){
						location.href="<%=basePath %>returnRequest/listReturnRequest.do";
					});
				}else{
					Prompt.alert("提示","采购申请保存失败！");
				}
			},
			error :function(){
				Prompt.alert("提示","采购申请保存失败！");
			}
		});
	}
	
	function saveAndSubmit(){
		if(!validateForm()) return ;
		$.ajax({
			url:"<%=basePath %>returnRequest/saveAndSubmit.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#returnRequestDetailForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购申请提交成功！",function(){
						location.href="<%=basePath %>returnRequest/listReturnRequest.do";
					});
				}else{
					Prompt.alert("提示","采购申请提交失败："+data.error);
				}
			},
			error :function(){
				Prompt.alert("提示","采购申请保存失败！");
			}
		});
	}
	</script>
</body>
</html>